/* $RCSFile: ColorDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/ColorDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Globalization;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Drawing;


using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// ColorDataType, parsed as a double
	/// </summary>
	public class ColorDataType : BaseDataType
	{

		public ColorDataType()
		{
			InnerDataType	=   PropertiesDataType.Unit;
			innerValue		=   "Transparent";
		}

		public ColorDataType(string defaultValue)
		{
			InnerDataType   =   PropertiesDataType.Unit;
			innerValue		=   defaultValue;
		}

        protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType InitializeComponents Creating ColorPicker for datatype description: {0}, UseRadControl: {1}", Description,_UsesRadControl),DataTypeTraceSwitch.Sw.Info);
            if (!UseRadControl)
            {
                base.InitializeComponents();
                return;
            }
            RadColorPicker tx                           =   new RadColorPicker();
            tx.Width                                    =   controlWidth;
            if (HandleChanges)
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]ColorDataType InitializeComponents HandleChanges set",DataTypeTraceSwitch.Sw.Info);
                tx.AutoPostBack                         =   true;
                tx.ColorChanged                         +=  new EventHandler(tx_ColorChanged);
            }
            if ( ReadOnly )
                tx.Enabled                              =   false;
            innerControl                                =   tx;
            _IsInitialized                              =   true;
		}

        /// <summary>
        /// Inform all subscribers  when text is changed, handler must have been installed
        /// </summary>
        void tx_ColorChanged(object sender,EventArgs e)
        {
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType tx_ColorChanged sender: {0}",sender),DataTypeTraceSwitch.Sw.Info);
            string oldValue         =   Val;
            ReadDataFromEditControl();
            string newValue         =   Val;
            OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.Changed,sender,e,oldValue,newValue));
        }

        public override void DataBind()
        {
			try
			{
                if ( !UseRadControl )
                    base.DataBind();
                else
                {
                    ( (RadColorPicker)innerControl ).SelectedColor = ColorTranslator.FromHtml(Value2Save);
                }
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ColorDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }

        /// <summary>
        /// Update value from control: used by eventhandler after a control change event triggered
        /// </summary>
        public override  string ReadDataFromEditControl()
		{
            if ( !UseRadControl)
                return base.ReadDataFromEditControl();
            Control Ctl                 =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl ==   null )
                return Val;
            if ( Ctl is RadColorPicker)
            {
                RadColorPicker Tx       =   (RadColorPicker)Ctl;
                innerValue              =   ColorTranslator.ToHtml(Tx.SelectedColor);
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl UseRadControl Set Value: {0}", innerValue),DataTypeTraceSwitch.Sw.Info);
            }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }
        
        public override Control EditControl
		{
			get
			{
                if ( !UseRadControl)
                    return base.EditControl;
                bool iCtrlDefined   =    ( innerControl != null );
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl GET iCtrlDefined : {0}", iCtrlDefined ),DataTypeTraceSwitch.Sw.Info);
				if ( !iCtrlDefined )
					InitializeComponents();
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl Get Value2Save: {0}", Value2Save),DataTypeTraceSwitch.Sw.Info);
				return innerControl;
			}
			set
			{
                if (!UseRadControl)
                {
                    base.EditControl                =   value;
                    return;
                }
                bool iCtrlDefined                   =    ( innerControl != null );
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl SET iCtrlDefined : {0}", iCtrlDefined ),DataTypeTraceSwitch.Sw.Info);
                bool iCtrlDefinedEqual              =   ( iCtrlDefined && innerControl.Equals(value) );
			    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl SET innerControl equal to value : {0}", iCtrlDefinedEqual ),DataTypeTraceSwitch.Sw.Info);
                if (value is RadColorPicker)
                {
                    if (!iCtrlDefinedEqual)
                    {
                        innerControl                =   value;
                        if (HandleChanges)
                        {
                            RadColorPicker Tx       =   (RadColorPicker)value;
                            Tx.AutoPostBack         =   true;
                            EventHandler evh        =   new EventHandler(tx_ColorChanged);
                            Tx.ColorChanged         -=  evh;
                            Tx.ColorChanged         +=  evh;
                        }
                    }
                    //Update value from control
                    // DO NOT use Value but use InnerValue
                    innerValue                      =   ColorTranslator.ToHtml(((RadColorPicker)innerControl).SelectedColor);
                    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ColorDataType EditControl UseRadControl Set Value: {0}", innerValue),DataTypeTraceSwitch.Sw.Info);
                    return;
                }
				string msg                          =   string.Format("[E]ColorDataType Set UseRadControl EditControl: {0}, not RadNumericTextBox",value.GetType());
				if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(msg);
			}
		}

        public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				try
				{
					innerValue  = value;
				}
				catch (Exception ex)
				{
					if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"ColorDataType Set Value: {0}, ex: {1}",value,ex),DataTypeTraceSwitch.Sw.Info);
				}
			}
		}

		public override string Description
		{
			get
			{
				return "Color";
			}
		}
	}
}